package com.example.supernote.ui.trash


import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import com.example.supernote.R
import com.example.supernote.model.manager.AbstractFileComposite
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.supernote.ui.components.UniformIcon

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TrashScreen(
    uiState: TrashUiState,
    search: (String) -> Unit,
    completelyDelete: (file: AbstractFileComposite) -> Unit,
    recovery : (file: AbstractFileComposite) -> Unit,
    isExpandedScreen: Boolean,
    openDrawer: () -> Unit,
    snackbarHostState: SnackbarHostState
) {
    var searchPattern by remember {
        mutableStateOf("")
    }
    Scaffold(
        snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
        topBar = {
            CenterAlignedTopAppBar(title = {
                TextField(
                    value = searchPattern,
                    onValueChange = {
                        searchPattern = it
                        search(searchPattern)
                    },
                    modifier = Modifier
                        .fillMaxWidth(),
                    placeholder = {
                        Text(
                            text = stringResource(id = R.string.cd_searchContent), color = Color.Gray
                        )
                    },
                    suffix = {
                        UniformIcon(imageVector = Icons.Filled.Search)
                    },
                    singleLine = true,
                    colors = TextFieldDefaults.colors(unfocusedContainerColor = MaterialTheme.colorScheme.background)
                )
            }, modifier = Modifier.padding(top = 5.dp, bottom = 5.dp), navigationIcon = {
                if (!isExpandedScreen) {
                    IconButton(onClick = openDrawer) {
                        Icon(
                            painter = painterResource(R.drawable.ic_super_note_logo),
                            contentDescription = stringResource(
                                R.string.cd_open_navigation_drawer
                            ),
                            modifier = Modifier.size(32.dp),
                            tint = MaterialTheme.colorScheme.primary
                        )
                    }
                }
            })
        }
    ) { innerPadding ->
        val screenModifier = Modifier.padding(innerPadding)
        TrashContent(
            uiState = uiState,
            completelyDelete = completelyDelete,
            recovery=recovery,
            modifier = screenModifier
        )
    }
}


//@Preview(showBackground = true)
//@Composable
//fun PreviewTrashScreen() {
//    val viewModel = TrashViewModel(FakeTrashRepository())
//    val uiState = viewModel.uiState.collectAsStateWithLifecycle()
//    SuperNoteTheme {
//        TrashScreen(
//            uiState = uiState,
//            search = { viewModel.search(it) },
//            completelyDelete = { viewModel.completelyDelete(it) },
//            recovery = { viewModel.recovery(it) },
//            isExpandedScreen = false,
//            openDrawer = { },
//            snackbarHostState = SnackbarHostState()
//        )
//    }
//}